package com.nexgo.payment.trans.eclog;

import android.support.annotation.NonNull;

import com.nexgo.payment.aop.annotation.TransCondition;
import com.nexgo.payment.trans.TransFlow;
import com.nexgo.data.entity.RecvData;
import com.nexgo.lamp.LampOperate;
import com.nexgo.oaf.apiv3.SdkResult;
import com.nexgo.oaf.apiv3.emv.EmvCardLogEntity;
import com.nexgo.oaf.apiv3.emv.EmvChannelTypeEnum;
import com.nexgo.oaf.apiv3.emv.EmvProcessResultEntity;
import com.nexgo.oaf.apiv3.emv.EmvTransDataEntity;
import com.nexgo.payment.App;
import com.nexgo.payment.R;
import com.nexgo.payment.constrant.CardMode;
import com.nexgo.payment.constrant.TransType;
import com.nexgo.payment.databases.DataSource;
import com.nexgo.payment.entity.CardInfo;
import com.nexgo.payment.entity.TransData;
import com.nexgo.payment.trans.AbsTransPresenter;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

import static com.google.common.base.Preconditions.checkNotNull;

/**
 * 电子现金 明细查询
 * <p>
 * Created by wanghongyang on 2017/8/31.
 */
@TransCondition
@TransFlow(checkNeedElectronicSignature = false,checkNeedReceipt = false)
public class EcLogPresenter extends AbsTransPresenter implements EcLogContract.Presenter {
    private Logger mLog = LoggerFactory.getLogger(EcLogPresenter.class.getSimpleName());

    private EcLogContract.View mView;

    public EcLogPresenter(@NonNull EcLogContract.View view, @NonNull DataSource repository) {
        super(view, repository);
        mView = checkNotNull(view);
        mView.setPresenter(this);
    }


    @Override
    protected TransData getTransData() {
        TransData transData = new TransData();
        transData.setTransType(TransType.EC_LOG);
        return transData;
    }

    @Override
    protected EmvTransDataEntity getEmvTransData(EmvChannelTypeEnum channelType) {
        return null;
    }

    @Override
    protected void onFirstAction() {
        postMainThread(() -> mView.showReadCard(mTransData));
    }

    @Override
    protected void readCardNext(Object data) {
        CardInfo cardInfo = (CardInfo) data;
        mTransData.setCardMode(cardInfo.getCardMode());
        EmvChannelTypeEnum channelType;
        //用上方式
        if (CardMode.INSERT == mTransData.getCardMode()) {
            channelType = EmvChannelTypeEnum.FROM_ICC;
            //接触式就关了灯
            LampOperate.getInstance().closeAll();
        } else {
            channelType = EmvChannelTypeEnum.FROM_PICC;
            LampOperate.getInstance().transOperateCard();
        }
        int result = mSdkEmvHandler.getEmvCardLog(channelType, new EmvProcessListener());
        if (result == SdkResult.Success) {
            postMainThread(() -> mView.setWaitingIndicator(true, App.sContext.getString(R.string.card_reading), App.sContext.getString(R.string.do_not_remove_card)));
        } else {
            postMainThread(() -> mView.showTransFailed());
        }
    }

    @Override
    protected void onTransSuccess(RecvData recvData, EmvProcessResultEntity emvResult) {
        if (emvResult == null) {
            postMainThread(() -> mView.showTransFailed());
            return;
        }
        List<EmvCardLogEntity> logEntityList = emvResult.getEmvLog();
        if (logEntityList == null || logEntityList.size() == 0) {
            postMainThread(() -> mView.showError(App.sContext.getString(R.string.has_no_trans_record)));
            return;
        }
        postMainThread(() -> mView.showEcLogList(logEntityList));
    }
}
